'''Author : seraph14'''
from cmath import isclose
from collections import defaultdict
import math
import sys, io, os
import heapq
if 'PyPy' in sys.version:
from _continuation import continulet
else:
import threading
input = sys.stdin.readline
hqp = heapq.heappop
hqs = heapq.heappush
def _print(*argv, sep=" "):
sys.stdout.write(sep.join(map(str, argv)) + "\n")
def inp():
return (int(input()))
def inlt():
return (list(map(int, input().split())))
def insr():
if input == sys.stdin.readline:
s = input()
return list(s.strip())
else:
s = input().decode()
return (list(s[:len(s)-2]))
def invr():
return (map(int, input().split()))
def get_bool(right):
return "YES" if right else "NO"
def solve():
pass
def main():
n, m, x = invr()
keyboard = []
for _ in range(n):
keyboard.append(insr())
q = inp()
T = insr()
key_to_coord = defaultdict(list)
key_to_dist = defaultdict(float)
for i in range(n):
for j in range(m):
key_to_coord[keyboard[i][j]].append((i, j))
for t in key_to_coord:
if t == "S":
continue
min_cost = float("inf")
for x1, y1 in key_to_coord[t]:
if "S" not in key_to_coord:
break
for x2, y2 in key_to_coord["S"]:
min_cost = min(min_cost, (x1-x2)**2 + (y1-y2)**2)
key_to_dist[t] = math.sqrt(min_cost) if min_cost != float("inf") else float("inf")
cost = 0
for t in T:
if (t.islower() and t not in key_to_coord) or (t.isupper() and t.lower() not in key_to_coord):
_print(-1)
return
if t.islower():
continue
if "S" not in key_to_coord:
_print(-1)
return
val = key_to_dist[t.lower()]
cost += 0 if math.isclose(val, x) or val < x else 1
_print(cost)
if __name__ == '__main__':
if 'PyPy' in sys.version:
def bootstrap(cont):
call, arg = cont.switch()
while True:
call, arg = cont.switch(
to=continulet(lambda _, f, args: f(*args), call, arg))
cont = continulet(bootstrap)
cont.switch()
main()
else:
sys.setrecursionlimit(1 << 30)
threading.stack_size(1 << 27)
main_thread = threading.Thread(target=main)
main_thread.start()
main_thread.join()
#include "bits/stdc++.h"
using namespace std;
int main(){
int n,m,x;
cin>>n>>m>>x;
char a[n][m];
bool flag=true;
vector<pair<int,int>> shift,poskey[26];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>a[i][j];
if(a[i][j]=='S'){
flag=false;
shift.emplace_back(i,j);
}
else{
poskey[a[i][j]-'a'].push_back(make_pair(i,j));
}
}
int q;
cin>>q;
string b;
cin>>b;
bool can[26];
memset(can,0,sizeof(can));
int ans=0;
for(int i=0;i<26;i++){
for(int j=0;j<poskey[i].size();j++){
for(auto [t,y]:shift){
if(sqrt(pow(t-poskey[i][j].first,2)+pow(y-poskey[i][j].second,2))<=x){can[i]=1; break;}
}
if(can[i]==1) break;
}
}
for(int i=0;i<q;i++){
if(poskey[tolower(b[i])-'a'].size()==0){cout<<"-1"; return 0;}
if(b[i]<='Z'){
if(flag){cout<<"-1"; return 0;}
if(can[b[i]-'A']==0) ans++;
}
}
cout<<ans;
return 0;
}
1363A - Odd Selection | 131B - Opposites Attract |
490C - Hacking Cypher | 158B - Taxi |
41C - Email address | 1373D - Maximum Sum on Even Positions |
1574C - Slay the Dragon | 621A - Wet Shark and Odd and Even |
1395A - Boboniu Likes to Color Balls | 1637C - Andrew and Stones |
1334B - Middle Class | 260C - Balls and Boxes |
1554A - Cherry | 11B - Jumping Jack |
716A - Crazy Computer | 644A - Parliament of Berland |
1657C - Bracket Sequence Deletion | 1657B - XY Sequence |
1009A - Game Shopping | 1657A - Integer Moves |
230B - T-primes | 630A - Again Twenty Five |
1234D - Distinct Characters Queries | 1183A - Nearest Interesting Number |
1009E - Intercity Travelling | 1637B - MEX and Array |
224A - Parallelepiped | 964A - Splits |
1615A - Closing The Gap | 4C - Registration System |